import * as express from 'express';
import * as bodyParser from 'body-parser';
import {TestDb} from './router/TestDb';
import {m4399} from './router/channel/m4399';
import { bilibili } from './router/channel/bilibili';
import { huawei } from './router/channel/huawei';
import { jinli } from './router/channel/jinli';
import { meizu } from './router/channel/meizu';
import { mi } from './router/channel/mi';
import { oppo } from './router/channel/oppo';
import { samsung } from './router/channel/samsung';
import { tencent } from './router/channel/tencent';
import { uc } from './router/channel/uc';
import { vivo } from './router/channel/vivo';
import { RouterType } from './router/BaseController';
import * as https from "https";
import * as http from 'http';
import * as fs from "fs";

let app = express();

// 获取post表单信息
app.use(bodyParser.urlencoded({ extended: true }));


app.get('/', (req, res) => res.send('NodeWzlz https - Node Server For Payment Callback Server'));
app.get("/test", (req, res) => {
    if (req.query.id != null){
        res.send('id = ' + req.query.id);
    }else{
        res.send('no input id');
    }
});

app.use(new TestDb().InitRouter("/testdb"));

app.use(new m4399().InitRouter("/wzlzPaymentCallback/m4399", RouterType.Get));
app.use(new bilibili().InitRouter("/wzlzPaymentCallback/bilibili", RouterType.Post));
app.use(new huawei().InitRouter("/wzlzPaymentCallback/huawei", RouterType.Post));
app.use(new jinli().InitRouter("/wzlzPaymentCallback/jinli", RouterType.Post));
app.use(new mi().InitRouter("/wzlzPaymentCallback/mi", RouterType.Get));
app.use(new uc().InitRouter("/wzlzPaymentCallback/uc", RouterType.Post));
app.use(new meizu().InitRouter("/wzlzPaymentCallback/meizu", RouterType.Post));
app.use(new samsung().InitRouter("/wzlzPaymentCallback/samsung", RouterType.Post));

app.use(new oppo().InitRouter("/wzlzPaymentCallback/oppo", RouterType.Post));
app.use(new vivo().InitRouter("/wzlzPaymentCallback/vivo", RouterType.Post));
app.use(new tencent().InitRouter("/wzlzPaymentCallback/tencent"));


// // 在最后加上ErrorHandler
// app.use(function (err: Error, req: express.Request, res: express.Response, next: express.NextFunction) {
//     console.error(err.stack);
//     res.status(500).send(req.url + ': Unknow Error');
// })
  
// app.listen(3000, () => console.log('Example app listening on port 3000!'));

// logMinute.invoke();

let options = {
    ca: fs.readFileSync('./ssl/tencent/ca.crt'),
    key: fs.readFileSync('./ssl/tencent/1106676599_2424670474152687409719975.key'),
    cert: fs.readFileSync('./ssl/tencent/1106676599_2424670474152687409719975.crt'),
    // key: fs.readFileSync('./ssl/privatekey.pem'),
    // cert: fs.readFileSync('./ssl/certificate.pem')
};

// https.createServer(options, app).listen(443, function () {
//     console.log('Https server listening on port: ' + 433);
// });

let httpServer = http.createServer(app);
let httpsServer = https.createServer(options, app);

httpServer.listen(8080);
httpsServer.listen(443);



